/*
 *  Copyright 2009 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
 
/* 
 *  This file is part of the WSF Staff project.
 *  Please, visit http://code.google.com/p/staff for more information.
 */

// This file generated by staff_codegen
// DO NOT EDIT

#include <rise/common/Exception.h>
#include <rise/common/MutablePtr.h>
#include <rise/common/ExceptionTemplate.h>
#include <map>
#include "$(Interface.Name)Context.h"
#include "$(Interface.Name)Impl.h"

#foreach $(Interface.Classes)
$(Class.OpeningNs)

  class $(Class.Name)Context::$(Class.Name)ContextImpl
  {
  public:
    typedef rise::CMutablePtr< $(Class.Name)Impl > P$(Class.ServiceName)Impl;
    typedef std::map<rise::CString, P$(Class.ServiceName)Impl> TServiceImplMap;
    TServiceImplMap m_mServices;
  };

  $(Class.Name)Context::$(Class.Name)Context():
    m_pImpl(new $(Class.Name)ContextImpl)
  {
  }

  $(Class.Name)Context::~$(Class.Name)Context()
  {
    if (m_pImpl != NULL)
      delete m_pImpl;
  }

  $(Class.Name)Context& $(Class.Name)Context::GetContext()
  {
    if (m_pInst == NULL)
      m_pInst = new $(Class.Name)Context;

    return *m_pInst;
  }

  $(Class.Name)Impl* $(Class.Name)Context::GetServiceImpl(const rise::CString& sID)
  {
    $(Class.Name)ContextImpl::TServiceImplMap::iterator itFind = m_pImpl->m_mServices.find(sID);
    if (itFind == m_pImpl->m_mServices.end())
    {
      const std::pair< $(Class.Name)ContextImpl::TServiceImplMap::iterator, bool >& tInsertResult = 
        m_pImpl->m_mServices.insert($(Class.Name)ContextImpl::TServiceImplMap::value_type(sID, $(Class.Name)ContextImpl::P$(Class.ServiceName)Impl(new $(Class.Name)Impl)));
      return tInsertResult.first->second;
    }

    return itFind->second;
  }

  const rise::CString& $(Class.Name)Context::GetServiceID(const $(Class.Name)Impl* pImpl) const
  {
    for($(Class.Name)ContextImpl::TServiceImplMap::const_iterator itService = m_pImpl->m_mServices.begin(); 
      itService != m_pImpl->m_mServices.end(); ++itService)
      if (itService->second == pImpl)
        return itService->first;

    RISE_THROWS(rise::CLogicNoItemException, "ID not found");
  }

  $(Class.Name)Context* $(Class.Name)Context::m_pInst = NULL;
  $(Class.EndingNs)

#end

